package com.diandian.backend.file.controller;

import com.diandian.backend.file.param.FIleUrlUploadParam;
import com.diandian.backend.file.service.FileRouterService;
import com.diandian.base.boot.starter.response.BaseResult;
import com.diandian.base.boot.starter.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(value = "/file/upload")
public class FileController {

    @Autowired
    private FileRouterService fileRouterService;

    /**
     * 文件上传到本地
     */
    @PostMapping("/localToLocal")
    public BaseResult localToLocal(MultipartFile file) {
        return ResultUtil.makeResult(0, "success", fileRouterService.uploadToLocal(file));
    }

    /**
     * 文件上传到oss
     */
    @PostMapping("/localToOSS")
    public BaseResult uploadToMinIO(MultipartFile file) {
        return ResultUtil.makeResult(0, "success", fileRouterService.uploadToOSS(file));
    }

    /**
     * 使用文件url上传到本地
     */
    @PostMapping("/urlToLocal")
    public BaseResult urlToLocal(@RequestBody FIleUrlUploadParam param) {
        param.check();
        return ResultUtil.makeResult(0, "success", fileRouterService.urlToLocal(param.getFileName(), param.getUrl()));
    }
}
